package com.hay.cart.controller;

import com.hay.cart.pojo.Cart;
import com.hay.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: CartController
 * @Author HuangYan
 * @Date: 2020/8/5 23:11
 * @Version 1.0
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 新增购物车
     * @param cart
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        cartService.addCart(cart);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("queryCarts")
    public ResponseEntity<List<Cart>> queryCarts(){
        List<Cart> carts = cartService.queryCarts();
        if (CollectionUtils.isEmpty(carts)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(carts);
    }

    /**
     * 修改商品数量
     * @param cart
     * @return
     */
    @PutMapping("editNum")
    public ResponseEntity<Void> editCartNum(@RequestBody Cart cart){
        cartService.editCartNum(cart);
        return ResponseEntity.noContent().build();
    }

    /**
     * 删除购物车中的商品
     * @param skuId
     * @return
     */
    @DeleteMapping("delete/{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId){
        cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }
}
